package com.thh.shirodemo.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.thh.shirodemo.bean.Goods;
import com.thh.shirodemo.mapper.GoodsMapper;
import com.thh.shirodemo.service.GoodsService;
import com.thh.shirodemo.util.RedisUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * 管理员登录后可以修改秒杀的活动
 * 一次活动只能有一件商品
 * @author AdrianHHT
 * @create 2022-07-28-15:35
 */
@RequestMapping("/manage")
@Controller
@RequiresPermissions("mobile")
public class SecKillManageController {
    @Resource
    GoodsService goodsService;

    @Autowired
    GoodsMapper goodsMapper;


    @Autowired
    RedisUtil redisUtil;
    /**
     * 秒杀页面
     */
    @GetMapping("/goodsTable")
    public String dynamic_table(@RequestParam(value="pn",defaultValue = "1") Integer pn,
                                Model model){

        //从数据库中查出user表中的用户进行展示

        //构造分页参数
        Page<Goods> page = new Page<>(pn,3);
        //调用page进行分页
        Page<Goods> userPage = goodsService.page(page, null);
        model.addAttribute("goods",userPage);
        model.addAttribute("goods",userPage);
        return "SecKillManage";
    }
    /**
     * 开启秒杀秒杀活动
     */
    @RequestMapping("/openThis/{goodName}")
    public String openThis(@PathVariable("goodName") String goodName,
                           @RequestParam(value="pn",defaultValue = "1") Integer pn,
                           RedirectAttributes ra,
                           HttpSession session){
        //RedirectAttributes ra重定向传递参数
        ra.addAttribute("pn",pn);
        //向前端传提示参数
        String msg;
        String secKillGood = goodsMapper.getSecKillGood();
        if(secKillGood!=null){
            msg="目前已经有秒杀活动正在进行!请先关闭后在开启!";
            session.setAttribute("SecKillMsg",msg);
            return "forward:/manage/goodsTable";
        }
        //1.1将商品表中的秒杀置为1(秒杀)
        Goods good = goodsMapper.getGoodsByName(goodName);
        good.setIsSeckill(1);
        goodsService.updateById(good);
        //更新redis中该商品的库存
        String kcKey="sk:"+good.getGoodsName()+"count";
        redisUtil.set(kcKey,good.getGoodsStock());
        msg="开启成功";
        session.setAttribute("SecKillMsg",msg);
        return "forward:/manage/goodsTable";
    }
    /**
     * 关闭秒杀活动
     */
    @RequestMapping("/closeThis/{goodName}")
    public String closeThis(@PathVariable("goodName") String goodName,
                           @RequestParam(value="pn",defaultValue = "1") Integer pn,
                           RedirectAttributes ra,
                           HttpSession session){
        //RedirectAttributes ra重定向传递参数
        ra.addAttribute("pn",pn);
        //向前端传提示参数
        String msg;
        Goods good = goodsMapper.getGoodsByName(goodName);
        if(good.getIsSeckill()==0){
            msg="该商品还没参加秒杀活动呢";
            session.setAttribute("SecKillMsg",msg);
            return "forward:/manage/goodsTable";
        }

        //拼接redis中的key
        String kcKey="sk:"+good.getGoodsName()+"count";
        String userKey="sk:"+good.getGoodsName()+"user";
        good.setIsSeckill(0);
        //同步mysql持久化中的余额数据(不在每次购买时 就更新 为了保证效率)
        good.setGoodsStock((Integer) redisUtil.get(kcKey));
        goodsMapper.updateById(good);

        //在redis中删除该活动 以保证以后再次再此开启该商品的秒杀后 抢到商品的用户还能继续抢购
        redisUtil.del(kcKey);
        redisUtil.del(userKey);
        msg="关闭成功";
        session.setAttribute("SecKillMsg",msg);
        return "forward:/manage/goodsTable";
    }
}
